#include <stdlib.h>

#include <iostream>

struct student {
	char password[16];
	char *name;
	int number;
	
	student() {
		for (int i = 0; i < 16; i++) {
			password[i] = 0;
		}
		
		name = nullptr;
		number = 0;
	}
};

typedef struct student Student;

Student *students[16] = {0};

void add() {
	int index = 0;
	std::cout << "index(0-f):";
	std::cin >> index;
	if (index > 15 || index < 0) {
		std::cout << "invalid index" << std::endl;
		return;
	}
	
	students[index] = new Student();
	std::cout << "input student number:";
	std::cin >> students[index]->number;
	if (students[index]->number > 255) {
		std::cout << "invalid number" << std::endl;
		return;
	}
	
	students[index]->name = (char *)malloc(students[index]->number);
	std::cout << "input student name:";
	std::cin >> students[index]->name;
	
	std::cout << "input student password:";
	std::cin >> students[index]->password;
	std::cout << "down" << std::endl;
}

void del() {
	int index = 0;
	std::cout << "index(0-f):";
	std::cin >> index;
	if (index > 15 || index < 0) {
		std::cout << "invalid index" << std::endl;
		return;
	}
	
	if (!students[index]) {
		std::cout << "student is null" << std::endl;
		return;
	}
	
	if (students[index]->name) {
		free(students[index]->name);
	}
	
	delete students[index];
	students[index] = nullptr;
}

void modify() {
	int index = 0;
	std::cout << "index(0-f):";
	std::cin >> index;
	if (index > 15 || index < 0) {
		std::cout << "invalid index" << std::endl;
		return;
	}
	
	if (!students[index]) {
		std::cout << "student is null" << std::endl;
		return;
	}
	
	std::cout << "input new student name:";
	std::cin >> students[index]->name;
}

void check() {
	int index = 0;
	std::cout << "index(0-f):";
	std::cin >> index;
	if (index > 15 || index < 0) {
		std::cout << "invalid index" << std::endl;
		return;
	}
	
	if (!students[index]) {
		std::cout << "student is null" << std::endl;
		return;
	}
	
	std::cout << "the student name:" << students[index]->name << std::endl;
}

int main(int argc, char *argv[]) {
	int choice = 0;
	while (1) {
		std::cout << "1,add" << std::endl;
		std::cout << "2,del" << std::endl;
		std::cout << "3,modify" << std::endl;
		std::cout << "4,check" << std::endl;
		std::cout << "5,quit" << std::endl;
		std::cout << "please input your choice:";
		std::cin >> choice;
		
		switch (choice) {
			case 1:
				add();
				break;
			case 2:
				del();
				break;
			case 3:
				modify();
				break;
			case 4:
				check();
				break;
			case 5:
				return 0;
			default:
				std::cout << "unknow" << std::endl;
				break;
		} 
	}
	
	return 0;
}
			

